<!--
 * @Author: lixb
 * @Date: 2020-08-26 17:54:33
 * @LastEditTime: 2020-09-23 17:53:30
 * @Descripttion: 编辑常用工具
-->

<template>
  <el-drawer
    title="编辑常用工具"
    :visible.sync="isShow"
    :direction="direction"
    :wrapper-closable="false"
    size="1000px"
    :before-close="beforeClose"
  >
    <div class="add-often-menu-row">
      <el-row>
        <el-col :span="12">
          <div class="often">常用工具</div>
          <div class="often-main">
            <div class="module-item">
              <div v-for="(item, index) in oftenList" :key="item.authNo" class="item-wrap">
                <img :src="item.authIcon" class="image" />
                <span class="el-icon-remove" @click="removeMenu(index)"></span>
                <div>{{ item.authName }}</div>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :span="12" class="all-menu-wrap">
          <div>
            <div v-for="(w, index) in menuList" :key="index">
              <div class="module-title">
                <div>{{ w.authName }}</div>
              </div>
              <div class="module-item">
                <div v-for="item in w.childZTrees" :key="item.authNo" class="item-wrap">
                  <img :src="item.authIcon" class="image" />
                  <span class="el-icon-circle-plus" @click="addMenu(item)"></span>
                  <div>{{ item.authName }}</div>
                </div>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-divider></el-divider>
    <div class="footer">
      <el-button @click="beforeClose()">取消</el-button>
      <el-button type="primary" :disabled="disabled" @click="save()">提交</el-button>
    </div>
  </el-drawer>
</template>

<script>
  import { getConfig } from '@/api/home';
  import { mapGetters } from 'vuex';
  import { setConfig } from '@/api/home';
  import { getLocalData } from '@/utils/localStore';

  export default {
    components: {},
    props: {
      isShow: {
        required: true,
        type: Boolean,
      },
    },
    data() {
      return {
        direction: 'rtl',
        oftenList: [],
        menuList: [],
        disabled: false,
      };
    },
    computed: {
      ...mapGetters('TagsView', ['tagsViewList']),
    },
    activated() {
      this.getOftenList();
      let list = (getLocalData('menuList') && getLocalData('menuList')[0]['childZTrees']) || [];
      this.menuList = list;
    },
    methods: {
      getOftenList() {
        getConfig(1).then(res => {
          const { data = {} } = res;
          this.oftenList = (data.menuConf && JSON.parse(data.menuConf)) || [];
        });
      },
      addMenu(obj) {
        if (this.oftenList.find(x => x.authUrl == obj.authUrl)) {
          this.$message.warning('不能重复添加');
        } else {
          this.oftenList.push(obj);
        }
      },
      removeMenu(index) {
        this.oftenList.splice(index, 1);
      },
      save() {
        this.disabled = true;
        let params = {
          menuConf: JSON.stringify(this.oftenList),
          menuType: 1,
        };
        setConfig(params).then(res => {
          if (res.status == 200) {
            this.$message.success('提交成功');
            this.beforeClose();
            this.$emit('refreshOften');
          }
          this.disabled = false;
        });
      },
      beforeClose() {
        this.$emit('closeDrawer');
      },
    },
  };
</script>

<style lang="scss" scoped>
  .all-menu-wrap {
    padding: 20px 20px 0 20px;
    overflow-y: auto;
    border-left: 1px solid #dcdfe6;
  }
  .el-divider {
    margin: 0;
  }
  .add-often-menu-row {
    height: calc(100vh - 150px);
    overflow-x: hidden;
    overflow-y: auto;
  }
  .footer {
    text-align: center;
    margin-top: 20px;
    .el-button {
      width: 150px;
    }
  }
  .often {
    margin: 20px;
    border-bottom: 3px solid #697bff;
    width: 57px;
  }
  .often-main {
    margin: 0 20px 20px 20px;
  }
  .module-title {
    font-size: 16px;
    font-weight: bold;
    margin: 20px 0 15px 0;
  }
  .module-item {
    .item-wrap {
      width: 100px;
      position: relative;
      display: inline-block;
      text-align: center;
      cursor: pointer;
      margin: 10px 0 10px 10px;
      white-space: nowrap;
      .el-icon-circle-plus,
      .el-icon-remove {
        position: absolute;
        top: 0;
        right: 0;
      }
      .image {
        width: 32px;
        height: 34px;
        margin: 10px 0 10px 0;
      }
    }
  }
</style>
